package com.tplan.tplanexternalserver.controller.serviceImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

import com.tplan.domain.Company;
import com.tplan.tplanexternalserver.controller.service.CompanyService;

@Service
public class CompanyServiceImpl implements CompanyService {

    private static String SELECT_COMPANY_BY_ID = "select id,name,introduce from company where id = :id";

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Company getCompanyById(int id) {
        Company company = new Company();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("id", id);
        namedParameterJdbcTemplate.query(SELECT_COMPANY_BY_ID, paramMap, new RowCallbackHandler() {

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                company.setId(id);
                company.setName(rs.getString("name"));
                company.setDescribe(rs.getString("introduce"));
            }
        });
        return company;
    }

}
